﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SmartProject.Entity;
using SmartProject.Data;

using NUnit.Framework;

namespace SmartProject.Testing
{
    [TestFixture]
    public class Test10_Tasks
    {
        [TestFixtureSetUp()]
        public void Start()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    BusinessRulePreferences.Username username =
                        new BusinessRulePreferences.Username();
                    username.SetSelectedBusinessRule(session, "Simple username");
                    User.NewUser(session, "David", "Lee", "dlee_Test10_Tasks", "dlee@test.com", "Any", new string[] { "User" });
                    transaction.Commit();
                }
            }
        }

        [Test]
        public void Test01_AddTask()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    Product product = new Product();
                    product.Description = "Some Product A";
                    product.Save(session);

                    Task task = Task.NewTask(session, "dlee_Test10_Tasks", product.Id,
                        Priority.GetPriority(session, "Normal").Id,
                        TaskCategory.GetTaskCategory(session, "Helpdesk ticket").Id,
                        "I am adding a new task. " + DateTime.Now.ToString());

                    transaction.Commit();
                }
            }
        }

        [Test]
        public void Test02_AddMessage()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    IList<Task> tasks = Task.GetOwnerTasks(session, "dlee_Test10_Tasks", "Priority", true);
                    Assert.AreEqual(1, tasks.Count);

                    Message message = Message.NewMessage(session, "dlee_Test10_Tasks", "New task is created", "Today a new task is created");
                    message.Save(session);
                    transaction.Commit();
                }
            }
        }

        [Test]
        public void Test03_VerifyMessage()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    IList<Task> tasks = Task.GetOwnerTasks(session, "dlee_Test10_Tasks", "Priority", true);
                    Assert.AreEqual(1, tasks.Count);

                    Task task = tasks[0];
                    User user = User.GetUserByUsername(session, "dlee_Test10_Tasks");
                    IList<Message> messages = user.GetNewMessages(session);
                    Assert.AreEqual(1, messages.Count);

                    Message message = messages[0];
                    Assert.IsNotNullOrEmpty(message.Subject);
                    Assert.IsNotNullOrEmpty(message.Description);
                }
            }
        }

        [Test]
        public void Test04_AddTaskAttachment()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    IList<Task> tasks = Task.GetOwnerTasks(session, "dlee_Test10_Tasks", "Priority", true);
                    Assert.AreEqual(1, tasks.Count);

                    Task task = tasks[0];
                    Attachment attachment = Attachment.NewAttachment(session, "dlee_Test10_Tasks",
                        System.Text.ASCIIEncoding.ASCII.GetBytes("ABCDEFGHI"), "new.txt");
                    task.Attachments.Add(attachment);
                    task.Save(session);
                    transaction.Commit();
                }
            }
        }

        [Test]
        public void Test05_VerifyAttachment()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    IList<Task> tasks = Task.GetOwnerTasks(session, "dlee_Test10_Tasks", "Priority", true);
                    Assert.AreEqual(1, tasks.Count);

                    Task task = tasks[0];
                    Assert.AreEqual(1, task.Attachments.Count);
                    Attachment attachment = task.Attachments[0];

                    Assert.AreEqual("new.txt", attachment.Filename);
                    Assert.AreEqual("ABCDEFGHI", System.Text.ASCIIEncoding.ASCII.GetString(attachment.Content));
                }
            }
        }

        [Test]
        public void Test06_DeleteTask()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    IList<Task> tasks = Task.GetOwnerTasks(session, "dlee_Test10_Tasks", "Priority", true);
                    Assert.AreEqual(1, tasks.Count);

                    Task task = tasks[0];
                    task.Delete(session);

                    transaction.Commit();
                }
            }
        }

        [TestFixtureTearDown()]
        public void End()
        {
            using (var session = DataProvider.Instance.Session)
            {
                User user = User.GetUserByUsername(session, "dlee_Test10_Tasks");

                using (var transaction = session.BeginTransaction())
                {
                    user.Remove(session);
                    User.Clean(session);
                    transaction.Commit();
                }
            }
        }
    }
}
